package com.recruit.wechat.config.shiro;

import com.alibaba.fastjson.JSONObject;
import com.recruit.wechat.controller.BaseController;
import org.apache.shiro.web.servlet.AdviceFilter;
import org.springframework.context.annotation.Configuration;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;


@Configuration
public class ShiroInterceptor extends AdviceFilter {
//    private static final List<String> USER_LOGIN = new ArrayList();
//
//    static {
//        USER_LOGIN.add("/WeChatUser/wechatUser/login");
//
//    }

    @Override
    protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {
//        HttpServletRequest request1 = (HttpServletRequest) request;
//
//
//        String requestURL = request1.getRequestURI();
//
//
//        System.out.println("requestURI:" + requestURL);
//            if (USER_LOGIN.contains(requestURL)){
//                return true;
//            }
//
//        HttpSession session = ((HttpServletRequest) request).getSession();
//        if (session.getAttribute("user") == null) {
//            response.setCharacterEncoding("UTF-8");
//            response.setContentType("application/json; charset=utf-8");
//            PrintWriter out = null;
//            try {
//                JSONObject res = new JSONObject();
//                res.put("success", "false");
//                res.put("msg", "用户没有登录");
//                out = response.getWriter();
//                out.append(res.toString());
//                return false;
//            } catch (Exception e) {
//                e.printStackTrace();
//                ((HttpServletResponse) response).sendError(500, "服务器异常");
//                return false;
//
//            }

//        }
        return true;
    }


    @Override
    public void afterCompletion(ServletRequest request, ServletResponse response, Exception exception) throws Exception {

//        String localAddr = request.getLocalAddr();
//        System.out.println("localAddr:"+localAddr);
//        int localPort = request.getLocalPort();
//        System.out.println("localPort:"+localPort);
//
//
//                HttpServletRequest request1 =(HttpServletRequest)request;
//
//        String requestURI = request1.getRequestURI();
//
//        System.out.println("requestURI:"+requestURI);
//        StringBuffer stringBuffer=request1.getRequestURL();
//
//        System.out.println("stringBuffer:"+stringBuffer);
//        String queryString = request1.getQueryString();
//
//        System.out.println("queryString:"+queryString);
//
//


//        String contextPath = ((HttpServletRequest) request).getContextPath();

//        System.out.println("contextPath:"+contextPath);

    }
}
